﻿<%@ webhandler language="C#" class="file" %>

using System;
using System.Web;

public class file : IHttpHandler, System.Web.SessionState.IRequiresSessionState {
    public void ProcessRequest(HttpContext context) {
		ent.TableFiles t4 = new ent.TableFiles();
		int qsId = System.Convert.ToInt32(context.Request.QueryString["id"]);
		
		System.Data.DataRow dr1 = t4.dynamicSqlSelect(null, ent.TableFiles.TBL__files, ent.TableFiles.TBL__files___PK__file_id + " = " + qsId).Rows[0];
		Byte[] fileBuffer = (Byte[])dr1[ent.TableFiles.TBL__files__file_binary];
		int fileLength = (int)dr1[ent.TableFiles.TBL__files__file_size_bytes];
		
		System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
		sb1.AppendFormat("attachment;filename={0}.{1}", dr1[ent.TableFiles.TBL__files__file_name_slug], dr1[ent.TableFiles.TBL__files__file_extension]);		
		
		// Output.
		context.Response.ClearHeaders();
		context.Response.ClearContent();
		context.Response.Buffer = false; // For large files.
		context.Response.Expires = 0;
		context.Response.ContentType = "application/octet-stream";
		context.Response.AddHeader("content-length", fileBuffer.Length.ToString());
		context.Response.AddHeader("content-disposition", sb1.ToString());
		context.Response.BinaryWrite(fileBuffer);
		context.Response.End();
    }
	public bool IsReusable {
		get {return false;}
	}
}